package the.broccoli.uc.service.impl;

import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.uc.mapper.RoleMapper;
import the.broccoli.uc.entity.Role;
import the.broccoli.uc.service.IRoleService;
import the.broccoli.uc.vo.RoleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2018-10-29
 */
@Service
public class RoleServiceImpl extends AbstractSuperService<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;

    /**
     * 新增角色
     *
     * @param roleName     角色名称
     * @param roleCode     角色编号
     * @param remark       备注
     * @param operatorId   操作人ID
     * @param operatorName 操作人名称
     */
    @Transactional
    @Override
    public boolean save(String roleName, String roleCode, String remark, String operatorId, String operatorName, String tenantId) {
        Role entity = new Role();
        entity.setTenantId(tenantId);
        entity.setRoleName(roleName);
        entity.setRoleCode(roleCode);
        entity.setRemark(remark);
        entity.setCreatedTime(new Date());
        entity.setCreatedId(operatorId);
        entity.setCreatedName(operatorName);
        entity.setDisabled(0);
        entity.setIsDeleted(0);
        return save(entity);
    }

    /**
     * 修改角色
     *
     * @param roleId       角色ID
     * @param roleName     角色名称
     * @param roleCode     角色编码
     * @param remark       备注
     * @param operatorId   操作人ID
     * @param operatorName 操作人名称
     * @return
     */
    @Override
    public boolean update(String roleId, String roleName, String roleCode, String remark, String operatorId, String operatorName) {
        Role entity = new Role();
        entity.setRoleId(roleId);
        entity.setRoleName(roleName);
        entity.setRoleCode(roleCode);
        entity.setRemark(remark);
        entity.setModifiedId(operatorId);
        entity.setModifiedName(operatorName);
        entity.setModifiedTime(new Date());
        return updateById(entity);
    }

    /**
     * 分页查询
     *
     * @param vo        查询条件
     * @param pageIndex 页码
     * @param pageSize  每页记录大小
     * @return 查询结果列表
     */
    @Override
    public IPage selectPageVO(RoleVO vo, Long pageIndex, Long pageSize) {
        return roleMapper.selectPageVO(new Page(pageIndex, pageSize), vo);
    }

}
